﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;

public partial class find_linkman : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
        }
        else
        {
            TUser.CheckUser(this);

            //设定状态
            if ((this.Session["ecrm_email"] == null) || (this.Session["ecrm_email"].ToString() == ""))
            {
                userstatus1.Text = "你好，请 <a href=\"main.aspx\">登录</a>或 ";
                userstatus2.Text = "<a href=\"./user/user_reg.aspx\">注册</a>";
            }
            else
            {
                string sid = this.Session["ecrm_uid"].ToString();
                TUser oUser = new TUser(sid);
                oUser.Select();
                string bigname = oUser.BigName;

                userstatus1.Text = "<a href=\"./user/user_myself.aspx\">" + bigname + "的设置</a>";
                userstatus2.Text = "<a href=\"./user/user_logout.aspx\">退出</a>";

                string fcode = this.Request["fcode"].ToString();
                string svalue = this.Request["svalue"].ToString();
                string ucdoe = this.Request["ucode"].ToString();
                findcode.Text = fcode;
                txtName.Text = svalue;
                unitcode.Text = ucdoe;

                if (ucdoe == "")
                {
                    lookrange.Text = "在自己的所有单位中查找...";
                }
                else
                {
                    TSysInfo2 oInfo = new TSysInfo2(ucdoe);
                    oInfo.Select();
                    lookrange.Text = "在自己的"+oInfo.InfoName+"中查找...";
                }

                if (svalue == "")
                {
                    svalue = "nolinkman";
                }

                ListMans(fcode, svalue, ucdoe);
            }
        }
    }

    private void ListMans(string fcode,string svalue,string unitcode)
    {
        string uid = this.Session["ecrm_uid"].ToString();
        string sSql = "";
        switch (fcode)
        {
            case "a":
                sSql = "select uid,linkman,sex,companyabb from t_linkman where linkman like '%" + svalue + "%' order by iid desc";
                break;
            case "b"://自己的全部单位
                sSql = "select uid,linkman,sex,companyabb,age,dbo.getinfo2byid(unittype) as unittype,dept,jobposi,mobile,tel,email,fax,company,companyaddr,companypostcode,favr,notice from t_linkman where linkman like '%" + svalue + "%' and inman='" + uid + "' order by iid desc";
                break;
            case "c"://自己的指定单位
                sSql = "select uid,linkman,sex,companyabb,age,dbo.getinfo2byid(unittype) as unittype,dept,jobposi,mobile,tel,email,fax,company,companyaddr,companypostcode,favr,notice from t_linkman where linkman like '%" + svalue + "%' and inman='" + uid + "' and unittype='" + unitcode + "' order by iid desc";
                break;
        }

        TSqlDBReader myReader = new TSqlDBReader(sSql);
        dlRS.DataSource = myReader.FDataReader;
        dlRS.DataBind();
        //关闭
        myReader.Free();

        ltcount.Text = dlRS.Items.Count.ToString();
    }

    protected void btnFind_Click(object sender, EventArgs e)
    {
        if (txtName.Text == "")
        {
            ListMans(findcode.Text, "nolinkman", unitcode.Text);
        }
        else
        {
            ListMans(findcode.Text, txtName.Text, unitcode.Text);
        }
    }
}
